package priv.mill.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import priv.mill.controller.util.RetenObj;
import priv.mill.entry.SysUserRole;
import priv.mill.server.SysUserRoleService;

@Controller
@RequestMapping("SysUserRole")
public class SysUserRoleController {

	@Autowired
	private SysUserRoleService sysUserRoleService;

	private static final Logger logger = Logger.getLogger(SysUserRoleController.class);

	@RequestMapping("getSysUserRoleById")
	@ResponseBody
	public RetenObj getSysUserRoleById(@RequestParam("SysUserRoleId") Integer sysUserRoleId) {
		try {
			return warpData(sysUserRoleService.getSysUserRoleById(sysUserRoleId), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	private RetenObj warpData(Object object, int i) {
		RetenObj retenObj = new RetenObj();
		retenObj.setData(object);
		retenObj.setStatus(i);
		return retenObj;
	}

	@RequestMapping("getSysUserRoleByEntry")
	@ResponseBody
	public RetenObj getSysUserRoleByEntry(SysUserRole sysUserRole) {
		try {
			return warpData(sysUserRoleService.getSysUserRoleByEntry(sysUserRole), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("getSysUserRolesByPage")
	@ResponseBody
	public RetenObj getSysUserRolesByPage(SysUserRole sysUserRole) {
		try {
			return warpData(sysUserRoleService.getSysUserRolesByPage(sysUserRole), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("updateSysUserRole")
	@ResponseBody
	public RetenObj updateSysUserRole(SysUserRole sysUserRole) {
		try {
			int result = sysUserRoleService.updateSysUserRole(sysUserRole);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}
	

	@RequestMapping("insertSysUserRole")
	@ResponseBody
	public RetenObj insertSysUserRole(SysUserRole sysUserRole) {
		try {
			int result = sysUserRoleService.insertSysUserRole(sysUserRole);
			if (result > 0)
				return warpData(sysUserRole.getUserRoleId(), 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("deleteSysUserRole")
	
	@ResponseBody
	public RetenObj deleteSysUserRole(Integer sysUserRoleId) {
		try {
			int result = sysUserRoleService.deleteSysUserRole(sysUserRoleId);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}

	}
}
